#include <pthread.h>
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;
#include "sys/thread.h"
#include "mem/nolock_queue.h"
#include "component/logger.h"
using namespace tdl;


//typedef nolock_queue<int> nlq_t;
typedef nolock_queue_ex<> nlvq_t;

//nlq_t nlq;
nlvq_t nlvq;

//size_logger log;
console_logger log;

////////////////
/*
void write_thread_main_4nlq()
{
	int i = 10;
	while(1)
	{
		bool ret = nlq.push_back(i);
		if(ret)
		{
			log.info() << "push=" << i << endl;
			++i;
		}
		else
			log.info() << "full" << endl;
		usleep(50);
	}
}

void read_thread_main_4nlq()
{
	while(1)
	{
		int i;
		bool ret = nlq.pop_front(i);
		if(! ret)
			log.info() << "empty" << endl;
		else
			log.info() << "pop=" << i << endl;
		usleep(50);
	}
}
*/
///////////////

void write_thread_main_4nlvq()
{
	int i = 10;
	ostringstream oss;
	while(1)
	{
		oss << i;
		string s = oss.str();
		oss.str("");
		uint16_t len = s.length();
		if(! nlvq.push_back(s.c_str(), len))
			log.info() << "full" << endl;
		else
		{
			log.info() << "push" << i << len << endl;
			++i;
		}
		usleep(50);
	}
}

void read_thread_main_4nlvq()
{
	while(1)
	{
		small_buffer<512> sb;
		sb.len = 512;
		if(nlvq.pop_front(sb.ptr, sb.len))
			log.info() << "pop" << string(sb.ptr, sb.len) << endl;
		else
			log.info() << "empty" << endl;
		usleep(50);
	}
}


//////////////////
int main(int argc, char* argv[])
{
	log.initialize();
//	logger_init_info lii("nolock_queue.log", 10000000, 100);
//	log.initialize(&lii, NULL, tdl::LOG_INFO, '|', true);

//	nlq.initialize(10);
	nlvq.initialize(10);

//	thread rdthr_4nlq(make_fun_exec(&read_thread_main_4nlq), false);
	thread rdthr_4nlvq(make_fun_exec(&read_thread_main_4nlvq), false);
//	thread wrthr_4nlq(make_fun_exec(&write_thread_main_4nlq), false);
	thread wrthr_4nlvq(make_fun_exec(&write_thread_main_4nlvq), false);

//	rdthr_4nlq.run();
	rdthr_4nlvq.run();
//	wrthr_4nlq.run();
	wrthr_4nlvq.run();

//	rdthr_4nlq.wait();
	rdthr_4nlvq.wait();
//	wrthr_4nlq.wait();
	wrthr_4nlvq.wait();

	return 0;
}

